package com.anji.plus.gaea.inf.util;

/**
 * @author ultrajiaming
 * @since 2021/3/18 10:10
 */
public class StrUtil {

    public static int indexOfIgnoreCase(String text, String search) {
        if (text == null || search == null || "".equals(text) || search.length() > text.length()) {
            return -1;
        }
        if ("".equals(search)) {
            return 0;
        }

        for (int i = 0; i < text.length(); i++) {
            if(text.regionMatches(true, i, search, 0, search.length())){
                return i;
            }
        }
        return -1;
    }

    public static boolean containsIgnoreCase(String text, String search) {
        if (text == null || search == null || "".equals(text) || search.length() > text.length()) {
            return false;
        }
        if ("".equals(search)) {
            return true;
        }
        for (int i = 0; i < text.length(); i++) {
            if(text.regionMatches(true, i, search, 0, search.length())){
                return true;
            }
        }
        return false;
    }

    public static String replaceIgnoreCase(String text, String source, String target) {
        if (text == null || source == null || "".equals(text) || source.length() > text.length()) {
            return text;
        }
        if ("".equals(source)) {
            return text;
        }
        for (int i = 0; i < text.length(); i++) {
            if(text.regionMatches(true, i, source, 0, source.length())){
                StringBuffer sb = new StringBuffer();
                sb.append(text.substring(0, i)).append(target).append(text.substring(i + source.length(), text.length()));
                return sb.toString();
            }
        }
        return text;
    }

}
